package com.magus.project.yddemo.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.entity.BaseTreeNode;
import com.magus.framework.core.entity.BaseTreeNode.TreeWithOutCheckedView;
import com.magus.framework.core.exception.app.ControllerException;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.webbean.TreeResult;
import com.magus.framework.system.entity.Company;
import com.magus.framework.system.service.CompanyService;
import com.magus.project.yddemo.service.IndicatorPackageService;
import com.magus.project.yddemo.vobean.IndicatorPackageVoBean;

import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @author Yingjian Wang
 * @date 2018-01-25 11:23
 */
@ApiIgnore
@RestController
@RequestMapping("/yddemo/indicatorPackageTree")
public class IndicatorPackageTreeController extends BaseController {

	@Autowired
	private IndicatorPackageService indicatorPackageService;

	@Autowired
	private CompanyService companyService;

	@GetMapping
	@JsonView(TreeWithOutCheckedView.class)
	ResponseEntity<TreeResult> findTree(@RequestParam("node") String node) {
		String[] node_param = StringUtils.split(node, "_");
		if (node_param.length != 2) {
			throw new ControllerException("请求参数出错：" + node);
		}

		String nodeType = node_param[0];
		String nodeId = node_param[1];

		TreeResult result = new TreeResult();
		nodeType = nodeType.toLowerCase().trim();
		if ("root".equals(nodeType)) {
			result.setChildren(findRootChilren());
		} else if ("company".equals(nodeType)) {
			result.setChildren(findCompanyChilren(nodeId));
		}

		return ResponseEntity.ok(result);
	}

	/**
	 * 获取ROOT子节点
	 */
	private List<BaseTreeNode> findRootChilren() {
		List<BaseTreeNode> nodes = new ArrayList<>();

		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("deleted", Boolean.FALSE));
		Specification<Company> spec = buildFilterRequest(searchFilters);

		List<Company> findAll = companyService.findAll(spec);
		for (Company _package : findAll) {
			_package.setNodePid("root_root");
			_package.setChecked(null);
			_package.setExpanded(false);
			_package.setLeaf(false);
			nodes.add(_package);
		}

		return nodes;
	}

	private List<BaseTreeNode> findCompanyChilren(String nodeId) {

		List<IndicatorPackageVoBean> allByCompany = indicatorPackageService
				.findVoBeanAllByCompany(companyService.findOne(nodeId));
		List<BaseTreeNode> collect = allByCompany.stream().map(temp -> {
			BaseTreeNode t = new BaseTreeNode();
			temp.setNodePid(nodeId);
			temp.setChecked(null);
			temp.setExpanded(false);
			temp.setLeaf(true);
			t = temp;
			return t;
		}).collect(Collectors.toList());
		return collect;
	}

}
